package com.throttle.raildroid;
import java.net.*;
import java.io.*;
import java.util.*;

class HelloWorldApp {
	/*private class msgReader implements Runnable{

		InputStreamReader in;
		msgReader(Socket sock){
			try {
				in = new InputStreamReader(sock.getInputStream());
			} catch (IOException e) {
				System.out.println("no input stream");
				e.printStackTrace();
			}
		}
		
		@Override
		public void run() {
			
			try {
				if(in.ready()){
					while(in.ready()){
						int size = in.read();
						assert size >= 0;
						if(size > 0){
							byte[] inmsg = new byte[size+2];
							inmsg[0] = (byte)size;
							inmsg[1] = (byte)in.read();
							for(int i=2; i<size+2; i++)
								inmsg[i] = (byte)in.read();
							System.out.println(MessageLibrary.decrypt(inmsg));
						}
						//System.out.print(in.read()+" ");
					}
					System.out.println("");
					System.out.println("");
				}
			} catch (IOException e) {
				System.out.println("Input Exception");
				e.printStackTrace();
			}
			
		}
	}//*/
	
    public static void main(String[] args) {
    	/*byte[] msg1 = new byte[6];
        byte sw_req = 0xB0-256;
        boolean bell = false;
        boolean horn  = false;
        boolean light  = false;
        boolean dir  = false;
        Queue<byte[]> myq = new LinkedList<byte[]>();//*/
    	
        System.out.println("Hello World!"); // Display the string.
        mainhelpclass f = new mainhelpclass();
        f.dothis();
        
        /*Socket mysocket = null;
        try {
        	//mysocket = new Socket("127.0.0.1", 14804);
        	//OutputStream out = mysocket.getOutputStream();
        	//BufferedReader in = new BufferedReader(new InputStreamReader(
        											//mysocket.getInputStream()));
        	String userInput = "";
        	BufferedReader stdIn = new BufferedReader(
                    new InputStreamReader(System.in));
        	
        	msg1[0] = 4;
        	msg1[1] = 0;
        	msg1[2] = sw_req;
        	System.out.print("Enter which train to take: ");
        	userInput = stdIn.readLine();
        	int  tr = Integer.parseInt(userInput);
        	
        	//mysocket = MessageLibrary.connect("127.0.0.1", 14804);
        	
        	
        	mysocket = MessageLibrary.connect("127.0.0.1", 14804);
        	HelloWorldApp.msgReader m = this.new msgReader(mysocket);
        	Thread t = new Thread(m);
        	
        	if(!MessageLibrary.controlTrain(tr))
        		System.out.println("Connection not set");
        	System.out.print("Enter Command (setspeed, changesw, setmisc). 'end' to end: ");
        	while(!(userInput = stdIn.readLine()).equals("end")){
        		if(userInput.equals("setspeed")){
        			System.out.print("Enter Speed: ");
        			userInput = stdIn.readLine();
        			int sp = Integer.parseInt(userInput);
        			if(!MessageLibrary.setSpeed(1, sp))
        				System.out.println("Connection not set");
        		}else if(userInput.equals("changesw")){
        			System.out.print("Enter switch to be changed 0-24: ");
        			userInput = stdIn.readLine();
        			int sw = Integer.parseInt(userInput);
        			System.out.print("Enter state (0/1): ");
        			userInput = stdIn.readLine();
        			int st = Integer.parseInt(userInput);
        			boolean ints = false;
        			if(st == 0)
        				ints = false;
        			else if(st == 1)
        				ints = true;
        			MessageLibrary.changeSwitch(sw, ints);
        		}else if(userInput.equals("setmisc")){
        			System.out.print("Enter what to change (horn/bell/mute/dir/light): ");
        			userInput = stdIn.readLine();
        			if(userInput.equals("horn")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, dir, false, bell, light);
        					horn = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, dir, true, bell, light);
        					horn = true;
        				}
        			}else if(userInput.equals("bell")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, dir, horn, false, light);
        					bell = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, dir, horn, true, light);
        					bell = true;
        				}
        			}else if(userInput.equals("light")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, dir, horn, bell, false);
        					light = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, dir, horn, bell, true);
        					light = true;
        				}
        			}else if(userInput.equals("dir")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0){
        					MessageLibrary.setMisc(1, false, horn, bell, light);
        					dir = false;
        				}else if (sth == 1){
        					MessageLibrary.setMisc(1, true, horn, bell, light);
        					dir = true;
        				}
        			}else if(userInput.equals("mute")){
        				System.out.print("Which state? ");
        				userInput = stdIn.readLine();
        				int sth = Integer.parseInt(userInput);
        				if(sth == 0)
        					MessageLibrary.setMute(1, false);
        				else if (sth == 1)
        					MessageLibrary.setMute(1, true);
        			}
        		}
        		
        		System.out.print("Enter Command (setspeed, changesw, setmisc). 'end' to end: ");
        	}
        	mysocket.close();
        	//out.close();
        }
        catch (java.net.UnknownHostException u) {
            System.out.println("uhe!"); // Display the string.
            System.err.println(u);
        }
        catch (java.io.IOException e) {
            System.out.println("ioex!"); // Display the string.
            System.err.println(e);
        }//*/
    }
    
    public static byte[] hexStringToByteArray(String s) { 
        int len = s.length(); 
        byte[] data = new byte[len / 2]; 
        for (int i = 0; i < len; i += 2) { 
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) 
                                 + Character.digit(s.charAt(i+1), 16)); 
        } 
        return data; 
    } 
    
    public static final byte[] intToByteArray(int value) {
        return new byte[] {
                (byte)(value >>> 24),
                (byte)(value >>> 16),
                (byte)(value >>> 8),
                (byte)value};
    }

    public static byte checkSum(byte[] msg){
    	byte chk = 0xFF-256;
    	for(int i=2; i<5; i++){
    		chk ^= msg[i];
    	}
    	return chk;
    }

}